package com.kaifamiao.exercises.methods;

public class Sorter {

    public void traversal( int[] array ){
        if( array != null && array.length > 0 ) {
            for( int i = 0 ; i < array.length ; i++ ){
                System.out.printf( "%d\t", array[i] );
            }
            System.out.println();
        }
    }

    public void sort( int[] array ){
        if( array != null && array.length > 0 ) {
            for( int i = 0 ; i < array.length - 1 ; i++ ) {
                for( int j = 0 ; j < array.length - 1 - i ; j++ ){
                    if( array[j] > array[j+1] ) {
                        array[j] ^= array[j+1];
                        array[j+1] ^= array[j];
                        array[j] ^= array[j+1];
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Sorter sorter = new Sorter();

        int[] a = { 1 , 100 , -20 , 99 , 1000 , 0 , 30 };
        System.out.print( "排序前 : " );
        sorter.traversal( a ); // 排序前 : 1 , 100 , -20 , 99 , 1000 , 0 , 30

        sorter.sort( a ) ;
        System.out.print( "排序后 : " );
        sorter.traversal( a ); // 排序后 : -20 , 0 , 1 , 30 , 99 , 100 , 1000
    }

}
